package com.infore.airsupervise.biz.impl;

import android.os.Environment;
import android.util.Log;

import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.ProgressBean;
import com.infore.airsupervise.biz.LoaderFileBiz;
import com.infore.airsupervise.http.HttpUtil;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.other.ProgressHandler;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.SocketTimeoutException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @features:
 * @author: WuTao
 * @date: 2017/5/10 15:23
 */
public class LoaderFileBizImpl implements LoaderFileBiz {
    private String filePath;
    private ProgressHandler mProgressHandler ;
    private ProgressBean progressBean;


    public LoaderFileBizImpl( ProgressHandler mProgressHandler  , ProgressBean progressBean) {
        File Folder = new File(Environment.getExternalStorageDirectory()+"/DCIM/AirSupervise/loader/");
        if(!Folder.exists()){
            Folder.mkdirs();
        }
        filePath =Folder.getAbsolutePath();
        this.mProgressHandler = mProgressHandler;
        this.progressBean = progressBean;
    }

    @Override
    public Call<ResponseBody> loaderFileRequest(final String name ,
                                                final OnServerResponseListener<String> listener) {

        Call call = HttpUtil.getLoadHttpService(mProgressHandler,progressBean).downloadFile("http://192.168.0.125:8080/WGHIMG/"+name);
        call.enqueue(new Callback<ResponseBody>(){
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                try {
                    InputStream is = response.body().byteStream();
                    long fileSize = response.body().contentLength() ;
                    Log.d("response.charStream", "onResponse: " +response.body().contentLength());
                    File file = new File(filePath+"/"+name);
                    FileOutputStream fos = new FileOutputStream(file);
                    BufferedInputStream bis = new BufferedInputStream(is);
                    byte[] buffer = new byte[1024];
                    int length;
                    long sum = 0;
                    while ((length = bis.read(buffer)) != -1) {
                        sum += length;
                        fos.write(buffer, 0, length);
                        fos.flush();

                    }
                    fos.close();
                    bis.close();
                    is.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    listener.onFailed(StatusConst.CODE_CONNECT_FAIL,"下载失败", "文件不存在");
                } catch (IOException e) {
                    e.printStackTrace();
                    listener.onFailed(StatusConst.CODE_CONNECT_FAIL,"下载失败", "文件流错误");
                } catch (NullPointerException e){
                    listener.onFailed(StatusConst.CODE_CONNECT_FAIL,"下载失败", "源文件可能不存在");
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                try {
                    throw t;
                } catch (ConnectException throwable) {
                    listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
                } catch (SocketTimeoutException throwable) {
                    listener.onFailed(StatusConst.CODE_CONNECT_TIMEOUT, MsgConst.TIME_OUT, MsgConst.PLEASE_CHECK_NETWORK);
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }
        });
        return call;
    }
}
